package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"os"
	"path/filepath"
	"strings"
	"unicode"
)

func main() {
	fmt.Printf("createTemplate")
	args := os.Args

	if len(args) == 1 {
		return
	}

}

func createModel(folder string, model string) {
	basePath, _ := os.Getwd()
	modelBasePath := filepath.Join(basePath, "application", "yiran", "app", "model")
	modelPath := filepath.Join(modelBasePath, folder)
	modelFullPath := filepath.Join(modelPath, model+".go")

	_, err := os.Stat(modelFullPath)
	if !os.IsNotExist(err) {
		fmt.Println("模型存在" + model + ".go")
		//return
	}

	//创建文件
	os.MkdirAll(modelPath, os.ModePerm)
	file, err := os.Create(modelFullPath)
	if err != nil {
		fmt.Println(model + ".go" + "模型文件创建失败")
		return
	}
	defer file.Close()

	//包名
	fs := strings.Split(folder, "/")
	packageName := fs[len(fs)-1]

	//struct名
	modelStruct := toPascalCase(model)

	//表结构
	columns := showTable(model)
	fmt.Printf("columns %v \n", columns)

	content := fmt.Sprintf("package %s\n", packageName)
	content += fmt.Sprintf("type %s struct {\n", modelStruct)
	content += fmt.Sprintf("}\n")
	//创建文件
	_, err = file.WriteString(content)
	if err != nil {
		fmt.Println(model + ".go" + "模型创建失败原因：" + err.Error())
		return
	}
	strings.ToUpper("")
	fmt.Println("模型创建成功")
}

// 字符串转成大驼峰
func toPascalCase(s string) string {
	var sep string
	if strings.Contains(s, "_") {
		sep = "_"
	} else {
		sep = "/"
	}
	words := strings.Split(s, sep)
	for i, word := range words {
		words[i] = string(unicode.ToUpper(rune(word[0]))) + word[1:]
	}
	return strings.Join(words, "")
}

// 获取表结构
func showTable(t string) []string {
	return nil
}
